<template>
  <div class="sales-view">
    <el-card shadow="hover" :body-style="{padding:'0 0 20px 0'}">
      <template #header>
        <div class="menu-wrapper">
          <el-menu class="sales-view-menu" @select="onMenuSelect" mode="horizontal" :default-active="activeIndex">
            <el-menu-item index="1">销售额</el-menu-item>
            <el-menu-item index="2">访问量</el-menu-item>
          </el-menu>
          <div class="menu-right">
            <el-radio-group v-model="radioSelect" size="small">
              <el-radio-button label="今日"></el-radio-button>
              <el-radio-button label="本周"></el-radio-button>
              <el-radio-button label="本月"></el-radio-button>
              <el-radio-button label="今年"></el-radio-button>
            </el-radio-group>
            <el-date-picker
              class="sales-date-picker"
              v-model="date"
              type="daterange"
              unlink-panels
              size="small"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions">
            </el-date-picker>
          </div>
        </div>
      </template>
      <template #default>
        <div class="sales-view-chart-wrapper">
          <!--Echarts图-->
          <div id="sales-view-chart"></div>
          <!--排行榜-->
          <div class="sales-view-list">
            <div class="sales-view-list-title">排行榜</div>
            <div class="list-item-wrapper">
              <div class="list-item" v-for="item in rankData" :key="item.no">
                <div class="list-item-no" :class="+item.no <=3 ? 'top-no':''">{{ item.no }}</div>
                <div class="list-item-name">{{ item.name }}</div>
                <div class="list-item-money">{{ item.money }}</div>
              </div>
            </div>
          </div>
        </div>
      </template>
    </el-card>
  </div>
</template>

<script>

export default {
  name: '',
  data() {
    return {
      // 当前选择的菜单
      activeIndex: '1',
      // 当前选择的单选框
      radioSelect: '今日',
      // 当前选择的时间
      date: null,
      // 时间快捷选项
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      // 排行榜数据
      rankData: [
        { no: 1, name: '重装机兵', money: '323,234' },
        { no: 2, name: '重装机兵', money: '323,234' },
        { no: 3, name: '重装机兵', money: '323,234' },
        { no: 4, name: '重装机兵', money: '323,234' },
        { no: 5, name: '重装机兵', money: '323,234' },
        { no: 6, name: '重装机兵', money: '323,234' },
        { no: 7, name: '重装机兵', money: '323,234' }
      ]
    }
  },
  mounted() {
    this.initChart()
  },
  methods: {
    // 菜单选择项改变
    onMenuSelect(index) {
      this.activeIndex = index
    },
    // 初始化echarts图表
    initChart() {
      const chartDom = document.getElementById('sales-view-chart')
      const chart = this.$echarts.init(chartDom, '', {
        renderer: 'svg'
      })
      chart.setOption({
        title: {
          top: '5%',
          left: 'center',
          text: '年度销售额',
          textStyle: {
            fontSize: this.$nowSize(16),
            color: '#666'
          }
        },
        grid: {
          top: 70,
          left: 60,
          bottom: 50,
          right: 60
        },
        xAxis: {
          type: 'category',
          data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
          // 轴线
          axisLine: {
            show: true,
            lineStyle: {
              color: '#999'
            }
          },
          // 轴的标志
          axisTick: {
            show: true,
            // 柱子对齐 axisTick
            alignWithLabel: true,
            lineStyle: {
              color: '#999'
            }
          },
          // 轴的字
          axisLabel: {
            color: '#333',
            fontSize: this.$nowSize(16)
          }
        },
        yAxis: {
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          splitLine: {
            lineStyle: {
              // 线的类型为虚线
              type: 'dotted',
              color: '#eee'
            }
          },
          // 轴的字
          axisLabel: {
            color: '#333',
            fontSize: this.$nowSize(16)
          }
        },
        // 两种方式都可以设置颜色
        // color: ['#3398DB'],
        series: [{
          type: 'bar',
          barWidth: '35%',
          // 两种方式都可以设置颜色
          itemStyle: {
            color: '#3398DB'
          },
          data: [200, 122, 123, 512, 623, 623, 622, 266, 761, 622, 121, 652]
        }]
      })
      window.addEventListener('resize', () => {
        chart.resize({
          animation: {
            duration: 500,
            easing: 'linear'
          }
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.sales-view {
  margin-top: 20px;

  .sales-view-menu {
    width: 100%;
    padding-left: 20px;

    .el-menu-item {
      height: 50px;
      line-height: 50px;
      margin: 0 20px;
    }
  }

  .menu-wrapper {
    position: relative;
    display: flex;

    .menu-right {
      position: absolute;
      right: 20px;
      height: 50px;
      display: flex;
      align-items: center;
      justify-content: flex-end;
    }

    .sales-date-picker {
      margin-left: 20px;
    }
  }

  .sales-view-chart-wrapper {
    display: flex;
    height: 270px;

    #sales-view-chart {
      flex: 1;
      height: 100%;
    }

    .sales-view-list {
      width: 360px;
      height: 100%;

      .sales-view-list-title {
        margin-top: 20px;
        font-size: 14px;
        color: #666666;
        font-weight: 600;
      }

      .list-item-wrapper {
        margin-top: 15px;

        .list-item {
          display: flex;
          align-items: center;
          font-size: 12px;
          height: 20px;
          padding: 6px 20px 6px 0;
        }

        .list-item-no {
          display: flex;
          align-items: center;
          justify-content: center;
          width: 20px;
          height: 20px;
          font-size: 12px;
          color: #333;

          &.top-no {
            background-color: #000000;
            border-radius: 50%;
            color: #FFFFFF;
            font-weight: 500;
          }
        }

        .list-item-name {
          margin-left: 10px;
          color: #333333;
        }

        .list-item-money {
          flex: 1;
          text-align: right;
        }
      }
    }
  }
}

</style>
